import os
from dotenv import load_dotenv
import sys

def check_env_vars():
    # Try to load from multiple locations
    env_paths = [
        ".env.local",
        "./backend/.env.local",
        "./backend/app/.env.local",
        os.path.abspath(".env.local"),
        os.path.join(os.path.dirname(__file__), ".env.local"),
    ]
    
    loaded = False
    for path in env_paths:
        if os.path.exists(path):
            print(f"Found .env file at: {path}")
            load_dotenv(path)
            loaded = True
            break
    
    if not loaded:
        print("Could not find .env.local file")
        print(f"Current directory: {os.getcwd()}")
        print(f"Available files: {os.listdir('.')}")
        return
    
    # Check for required variables
    keys = [
        "AZURE_OPENAI_API_KEY",
        "AZURE_OPENAI_API_VERSION",
        "AZURE_OPENAI_DEPLOYMENT"
    ]
    
    missing = []
    for key in keys:
        value = os.environ.get(key)
        if value:
            # Mask API key for security
            if key == "AZURE_OPENAI_API_KEY":
                masked = value[:4] + "..." + value[-4:] if len(value) > 8 else "***"
                print(f"{key}: {masked}")
            else:
                print(f"{key}: {value}")
        else:
            missing.append(key)
            print(f"{key}: MISSING")
    
    if missing:
        print(f"\nMissing {len(missing)} environment variables: {', '.join(missing)}")
    else:
        print("\nAll required environment variables are set")

if __name__ == "__main__":
    check_env_vars() 